package com.example.shopdemo.controller.wechat;

import com.example.shopdemo.utils.SignUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author ：Anitaxiaofeng
 * @description：
 * @create 2018-01-30
 **/
@Controller
@Slf4j
@RequestMapping("/wechat")
public class WeiXinController {

    @RequestMapping(method = {RequestMethod.GET})
    public void doGet(HttpServletRequest request, HttpServletResponse response){
        log.debug("weixin get...");
        //微信加密签名
        String signature = request.getParameter("signature");
        //时间戳
        String timestamp = request.getParameter("timestamp");
        //随机数
        String nonce = request.getParameter("nonce");
        //随机字符串
        String echostr = request.getParameter("echostr");
        //通过校验signature 对请求进行校验，若校验成功则原样返回echostr,表示接入成功，否则接入失败
        PrintWriter out = null;
        try{
            out = response.getWriter();
            if(SignUtil.checkSignature(signature,timestamp,nonce)){
                log.debug("weixin get success");
                out.print(echostr);
            }
        }catch(IOException e){
            e.printStackTrace();
        } finally{
            if(out!=null)
                out.close();
        }
    }






}
